﻿Imports System.IO
Imports System.Windows.Browser
Imports Microsoft.LightSwitch.Threading
Imports System.Net

Public Class ImageHelper
    Private Shared _currentUri
    Public Shared Property CurrentURI As Uri
        Get
            Return _currentUri
        End Get
        Set(value As Uri)
            _currentUri = value
        End Set
    End Property

    Public Shared Function GetImageByName(fileName As String) As Byte()
        ''Dim assembly As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly()

        ''This line returns the error: "File operation not permitted. Access to path 'C:\Work\vs2010\projects\Application1\Application1\bin\Debug\images\3040.jpg' is denied."
        If Not IsNothing(fileName) And Not fileName = String.Empty Then
            getURI()
            '    Dim context As System.Web.HttpContext = HttpContext.Current


            '    Dim req As WebRequest = WebRequest.Create(CurrentURI.ToString & fileName)
            '    Using resp As WebResponse = req.GetResponse()
            '        Using stream As Stream = resp.GetResponseStream()
            '            Dim outFile As String = "\" & fileName
            '            Dim fs As New FileStream(outFile, FileMode.Create)
            '            Dim buffer As Byte() = New Byte(resp.ContentLength - 1) {}
            '            stream.Read(buffer, 0, CInt(resp.ContentLength))
            '            fs.Write(buffer, 0, CInt(resp.ContentLength))
            '            fs.Close()
            '        End Using
            '    End Using



            Dim assembly As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly
            Dim stream As Stream = assembly.GetManifestResourceStream(CurrentURI.ToString & fileName)

            Dim fstream = New FileStream(".." & fileName, FileMode.Open, FileAccess.Read, FileShare.Read)
           

            Return GetStreamAsByteArray(fStream)
        Else
            Return Nothing
        End If


    End Function

    Private Shared Function GetStreamAsByteArray(ByVal stream As System.IO.Stream) As Byte()
        If stream IsNot Nothing Then
            Dim streamLength As Integer = Convert.ToInt32(stream.Length)
            Dim fileData(streamLength - 1) As Byte
            stream.Read(fileData, 0, streamLength)
            stream.Close()
            Return fileData
        Else
            Return Nothing
        End If
    End Function

    Private Shared Sub getURI()
        Dim hostUri As Uri = Nothing
        Dispatchers.Main.Invoke(
             Sub()
                 Dim strBaseWebAddress As String = System.Windows.Browser.HtmlPage.Document.DocumentUri.AbsoluteUri
                 ' Find the position of the ClientBin directory        
                 Dim PositionOfClientBin As Integer = System.Windows.Browser.HtmlPage.Document.DocumentUri.AbsoluteUri.ToLower.IndexOf("/default")
                 If (PositionOfClientBin > -1) Then
                     ' Strip off everything after the ClientBin directory         
                     strBaseWebAddress = Microsoft.VisualBasic.Strings.Left(strBaseWebAddress, PositionOfClientBin)
                 Else
                     strBaseWebAddress = (strBaseWebAddress + "\")
                 End If
                 ' Create a URI
                 CurrentURI = New Uri(String.Format("{0}", strBaseWebAddress))
             End Sub
            )
    End Sub
End Class